//
//  MineAdressViewController.m
//  Mahjong
//
//  Created by KING on 16/9/8.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "MineAdressViewController.h"
#import "MoreSelectView.h"
#import "Header.h"
#import "MineAdressTableViewCell.h"
#import "MineAdressTitleTableViewCell.h"
#import "MineAdressPreTableViewCell.h"
#import "MineAdressButtonTableViewCell.h"
#import "MineAdressDetailViewController.h"
#import "AddPhoneViewController.h"
#import "ListHomeModel.h"
@interface MineAdressViewController ()
<selectDelegate,UITableViewDelegate,UITableViewDataSource,goDelegate>
@property (nonatomic, strong) MoreSelectView *selectView;
@property (nonatomic, strong) NSArray *topArray;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) NSMutableArray *dateArray;
@end

@implementation MineAdressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
//    _selectView = [[MoreSelectView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
//    [_selectView.oneButton setTitle:@"购物订单" forState:UIControlStateNormal];
//     [_selectView.twoButton setTitle:@"定制订单" forState:UIControlStateNormal];
//    [self.view addSubview:_selectView];
//    _selectView.myDelegate = self;
    [self addTopView];
    self.dateArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 41, SCREEN_WIDTH, SCREENH_HEIGHT - 90) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineAdressTableViewCell" bundle:nil] forCellReuseIdentifier:@"oneCell"];
     [self.myTableView registerNib:[UINib nibWithNibName:@"MineAdressTitleTableViewCell" bundle:nil] forCellReuseIdentifier:@"titleCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineAdressPreTableViewCell" bundle:nil] forCellReuseIdentifier:@"preCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineAdressButtonTableViewCell" bundle:nil] forCellReuseIdentifier:@"buttonCell"];
  
    self.navigationItem.title = @"地址管理";
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    [self create:1];
}
-(void)create:(NSInteger)num{
   
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetCustomizedListForSellers&page=页码1&userid=%@&state=%ld",USERID,num] success:^(id result) {
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            ListHomeModel *model = [[ListHomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dateArray addObject:model];
            
        }
        [self.myTableView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
}
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    ListHomeModel *model = self.dateArray[indexPath.section];
    if (indexPath.row == 0) {
        MineAdressTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell"];
//        cell.numLabel.text = [NSString stringWithFormat:@"%ld", model.OrderId];
//        cell.timeLabel.text = model.addtime;
        return cell;
    }
    if (indexPath.row == 3) {
        MineAdressPreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"preCell"];
        
        return cell;
    }
    if (indexPath.row == 4) {
        MineAdressButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell"];
        cell.delegate = self;
        if (self.number == 100) {
            cell.titleImage.alpha = 0;
            cell.titleLabel.alpha = 0;
            cell.disButton.alpha = 1;
          cell.disButton.backgroundColor = [UIColor orangeColor];
            [cell.disButton setTitle:@"取消订单" forState:UIControlStateNormal];
            
        cell.okButton.backgroundColor = [UIColor redColor];
            [cell.okButton setTitle:@"付款" forState:UIControlStateNormal];
        }
        if (self.number == 101) {
           
            
            cell.titleImage.alpha = 0;
            cell.titleLabel.alpha = 0;
            cell.disButton.alpha = 0;
            cell.okButton.backgroundColor = [UIColor redColor];
            [cell.okButton setTitle:@"提醒发货" forState:UIControlStateNormal];
        }if (self.number == 102) {
           
            
            cell.titleImage.alpha = 1;
            cell.titleLabel.alpha = 1;
            cell.disButton.alpha = 0;
            [cell.okButton setTitle:@"确认收货" forState:UIControlStateNormal];
        }if (self.number == 103) {
           
            cell.titleImage.alpha = 1;
            cell.titleLabel.alpha = 1;
            cell.disButton.alpha = 0;
            [cell.okButton setTitle:@"去评价" forState:UIControlStateNormal];
        }
        
        
        return cell;
    }
    MineAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneCell"];
    return cell;
    
    
}
-(void)go{
    if (self.number == 103) {
        AddPhoneViewController *add = [[AddPhoneViewController alloc] init];
        [self.navigationController pushViewController:add animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 || indexPath.row == 3 || indexPath.row == 4) {
        return 40;
    }
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MineAdressDetailViewController *detail = [[MineAdressDetailViewController alloc] init];
    detail.didNum = self.number;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)oneSelectAction{
    
}
-(void)twoSelectAction{
    
}
-(void)addTopView{
    self.titleArray = @[@"待付款",@"待发货",@"待收货",@"待评价"];
    for (int i = 0; i <self.titleArray.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        button.frame = CGRectMake(i*SCREEN_WIDTH/self.titleArray.count, 2, SCREEN_WIDTH/self.titleArray.count, 42);
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.view addSubview:button];
        button.tag = 100+i;
        [button addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIButton *a = (UIButton *)[self.view viewWithTag:100];
    [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.number = 100;
}
-(void)topAction:(UIButton *)btn{
    
    if (btn.tag == 100) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 100;
        [self create:1];
        
    }
    if (btn.tag == 101) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 101;
        [self create:2];
        
    }
    if (btn.tag == 102) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 102;
        [self create:8];
        
    }if (btn.tag == 103) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 103;
        [self create:9];
        
    }
    if (btn.tag == 104) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        self.number = 104;
        
    }
    [self.myTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
